package com.ucode.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ucode.crm.mapper.BusinessStatusGroupMapper;
import com.ucode.crm.mode.BusinessStatus;
import com.ucode.crm.mode.BusinessStatusGroup;
import com.ucode.crm.mode.BusinessStatusGroupDept;
import com.ucode.crm.service.BusinessStatusGroupService;
import com.ucode.crm.vo.BusinessStatusGroupVo;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.IdentifyUtils;
import cn.hutool.core.collection.CollUtil;

@Service
public class BusinessStatusGroupServiceImpl implements BusinessStatusGroupService {

    @Autowired
    private BusinessStatusGroupMapper businessStatusGroupMapper;
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public BusinessStatusGroup insert(BusinessStatusGroupVo businessStatusGroupVo) {
        if(CollUtil.isEmpty(businessStatusGroupVo.getItems())){
            throw new UcodeServiceException(ResultCodeEnum.VALIDATE_FAILED);
        }
        Date now = new Date();
        BusinessStatusGroup businessStatusGroup = new BusinessStatusGroup();
        businessStatusGroup.setId(IdentifyUtils.getDefaultSnowflakeId());
        businessStatusGroup.setCreatedTime(now);
        businessStatusGroup.setUpdatedTime(now);
        businessStatusGroup.setName(businessStatusGroupVo.getName());
        businessStatusGroup.setStatuz(businessStatusGroupVo.getStatuz());
        
        if(businessStatusGroupMapper.insert(businessStatusGroup) == 0)
            throw new UcodeServiceException(ResultCodeEnum.FAILED);
        
        List<BusinessStatus> businessStatusItems = new ArrayList<>();
        for(BusinessStatus businessStatus : businessStatusGroupVo.getItems()){
            businessStatus.setId(IdentifyUtils.getDefaultSnowflakeId());
            businessStatus.setCreatedTime(now);
            businessStatus.setUpdatedTime(now);
            businessStatus.setGroupId(businessStatusGroup.getId());
            businessStatusItems.add(businessStatus);
        }
        
        businessStatusGroupMapper.batchSaveStatus(businessStatusItems);
        
        return businessStatusGroup;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int update(BusinessStatusGroupVo businessStatusGroupVo) {
        BusinessStatusGroup businessStatusGroup = new BusinessStatusGroup();
        businessStatusGroup.setId(businessStatusGroupVo.getId());
        businessStatusGroup.setName(businessStatusGroupVo.getName());
        businessStatusGroup.setStatuz(businessStatusGroupVo.getStatuz());
        
        if(businessStatusGroupMapper.update(businessStatusGroup) == 0)
            throw new UcodeServiceException(ResultCodeEnum.FAILED);
        
        List<Long> ids = businessStatusGroupMapper.findBusinessStatuzIds(businessStatusGroup.getId());
        if(CollUtil.isNotEmpty(ids))
            businessStatusGroupMapper.batchDeleteBusinessStatus(ids);
        
        if(CollUtil.isNotEmpty(businessStatusGroupVo.getItems())){
            Date now = new Date();
            List<BusinessStatus> businessStatusItems = new ArrayList<>();
            for(BusinessStatus businessStatus : businessStatusGroupVo.getItems()){
                businessStatus.setId(IdentifyUtils.getDefaultSnowflakeId());
                businessStatus.setCreatedTime(now);
                businessStatus.setUpdatedTime(now);
                businessStatus.setGroupId(businessStatusGroup.getId());
            }
            businessStatusGroupMapper.batchSaveStatus(businessStatusItems);
        }
        return 1;
    }

    @Override
    public int updateStatuz(Long id, Integer statuz) {
        BusinessStatusGroup businessStatusGroup = new BusinessStatusGroup();
        businessStatusGroup.setId(id);
        businessStatusGroup.setStatuz(statuz);
        return businessStatusGroupMapper.update(businessStatusGroup);
    }
    
    @Override
    public int relatedDepts(Long id, List<Long> deptIds) {
        List<BusinessStatusGroupDept> list =new ArrayList<>();
        Date now = new Date();
        for(Long deptId : deptIds){
            BusinessStatusGroupDept businessStatusGroupDept = new BusinessStatusGroupDept();
            businessStatusGroupDept.setId(IdentifyUtils.getDefaultSnowflakeId());
            businessStatusGroupDept.setDeptId(deptId);
            businessStatusGroupDept.setGroupId(id);
            businessStatusGroupDept.setCreatedTime(now);
            businessStatusGroupDept.setUpdatedTime(now);
            list.add(businessStatusGroupDept);
        }
        return businessStatusGroupMapper.batchSaveGroupDept(list);
    }
    
    @Override
    public int deleteRelatedDepts(List<Long> relatedIds) {
        return businessStatusGroupMapper.batchDeleteGroupDept(relatedIds);
    }


    @Override
    public int deleteByLogic(Long id) {
        return businessStatusGroupMapper.deleteByLogic(id);
    }

    @Override
    public int delete(Long id) {
        int i = businessStatusGroupMapper.delete(id);
        if(i > 0){
            List<Long> ids = businessStatusGroupMapper.findBusinessStatuzIds(id);
            if(CollUtil.isNotEmpty(ids))
                businessStatusGroupMapper.batchDeleteBusinessStatus(ids);
        }
        return i;
    }

    @Override
    public BusinessStatusGroup findById(Long id) {
        return businessStatusGroupMapper.findById(id);
    }

    @Override
    public List<BusinessStatusGroup> findByIds(List<Long> ids) {
        return businessStatusGroupMapper.findByIds(ids);
    }
    
    @Override
    public List<BusinessStatus> findBusinessStatuzs(Long groupId) {
        return businessStatusGroupMapper.findBusinessStatuzs(groupId);
    }
    
    @Override
    public List<BusinessStatusGroup> findDeptValidBusinessStatusGroup(Long depteId) {
        return businessStatusGroupMapper.findDeptValidBusinessStatusGroup(depteId);
    }

    @Override
    public Paginator<BusinessStatusGroup> queryPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BusinessStatusGroup> list = businessStatusGroupMapper.queryList();
        PageInfo<BusinessStatusGroup> pageInfo = new PageInfo<>(list);
        return new Paginator<>(pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), list);
    }

}
